<!doctype html>
<html>
  <head>
    <meta charset="utf-8">
    <title>Lines 3D 2</title>
    <link rel="stylesheet" href="../include/style.css">
  </head>
  <body>
    <header>
      Example from <a href="http://amzn.com/1430236655?tag=html5anim-20"><em>Foundation HTML5 Animation with JavaScript</em></a>
    </header>
    <canvas id="canvas" width="400" height="400"></canvas>
    <aside>Move mouse on canvas element.</aside>

    <script src="../include/utils.js"></script>
    <script src="./classes/point3d.js"></script>
    <script>
    window.onload = function () {
      var canvas = document.getElementById('canvas'),
          context = canvas.getContext('2d'),
          mouse = utils.captureMouse(canvas),
          points = [],
          numPoints = 50,
          fl = 250,
          vpX = canvas.width / 2,
          vpY = canvas.height / 2,
          angleX, angleY;

      for (var point, i = 0; i < numPoints; i++) {
        point = new Point3d(Math.random() * 200 - 100,
                            Math.random() * 200 - 100,
                            Math.random() * 200 - 100);
        point.setVanishingPoint(vpX, vpY);
        points.push(point);
      }

      function move (point) {
        point.rotateX(angleX);
        point.rotateY(angleY);
      }

      function draw (point, i) {
        if (i !== 0) {
          context.lineTo(point.getScreenX(), point.getScreenY());
        }
      }

      (function drawFrame () {
        window.requestAnimationFrame(drawFrame, canvas);
        context.clearRect(0, 0, canvas.width, canvas.height);
        
        angleX = (mouse.y - vpY) * 0.001;
        angleY = (mouse.x - vpX) * 0.001;
        points.forEach(move);
        context.beginPath();
        //line starts at first point
        context.moveTo(points[0].getScreenX(), points[0].getScreenY());
        points.forEach(draw);
        context.stroke();
      }());
    };
    </script>
  </body>
</html>
